/*
* 编辑正文操作
* */
import {useBaseInfoWithOut} from '@/store/modules/base-info';
import globalData, {BodyTypeEnums} from '@/plugins/data';
import globalMethods from '@/plugins/methods';
import emitter from "@/plugins/global-callback";
import {editOffice} from "@/plugins/office";
import {useSummaryTabWithOut} from "@/store/modules/summary/tab";

const useBaseInfo = useBaseInfoWithOut();
const summaryTab = useSummaryTabWithOut();

function updateHtmlContent() {
  let zwIframe = window._getZWIframe();
  const {isHasISignFlag=()=>false} =zwIframe;
  if (isHasISignFlag()){
    globalMethods.warning({
      title: globalMethods.i18n('common.system.hint.label'),
      content: globalMethods.i18n('collaboration.alert.CantModifyBecauseOfIsignature'),
    });
    return;
  }
  //当前正文内容,内容为空的时候会从数据库获取，这样就能刷新一次内容
  let content = '';
  if (window.isEditAttOrContent === 1 || window.isEditAttOrContent === 3) {
    content = zwIframe.$.content?.getCurrentContent();
  }
  let summaryId = useBaseInfo.businessData.summary.id;
  //添加正文锁
  const res = window.callBackendMethod("colManager", "writeLock", summaryId);
  const {id: userId} = useBaseInfo.businessData.currentUser;
  //获取锁失败且锁不属于自己
  if (res?.success!==true&&res?.lockInfo?.owner!==userId){
    globalMethods.warning({
      title: globalMethods.i18n('common.system.hint.label'),
      content: res.msg,
    });
    return;
  }
  const url = `${globalData.ctxPath}/collaboration/collaboration.do?method=updateContentPage&summaryId=${summaryId}`;
  var title = $.i18n('common.detail.label.editContent');
  //伪造dom对象，让修改正文的数据回填
  window.getA8Top().updataContentOBJ = [{
    document: {
      zwIframe: zwIframe,
      getElementById:()=> {
        return {
            contentWindow:zwIframe,
        }
      }
    },
    //保存完成回调
    setFlagto1: () => {
      //0 ： 没有做修改, 1:修改了正文   2：修改了附件 3 ： 修改了正文和附件
      if (window.isEditAttOrContent === 2) {
        window.isEditAttOrContent = 3;
      } else {
        window.isEditAttOrContent = 1;
      }
      //标记为修改状态不然不会入库保存
      zwIframe.$.content?.markModified();
      window.openFrom = globalData.openFrom;
      dialog.close();
      emitter.publish('ContentLoaded',{});
    },
    //关闭的回调
    setFlagto0:()=>{},
    close: () => dialog.close()
  }, content];
  const dialog = globalMethods.dialog({
    url,
    centered:true,
    width: window.top.innerWidth - 100,
    height: window.top.innerHeight - 100,
    id: 'dialogUpdate',
    bodyStyle:'padding:0',
    dialogStyle:'padding:0',
    title: title,
    transParams: [window, content],
    footer: null,
    onCancel() {
      dialog.close();
    },
    onClose: () => {
      dialog.close();
    }
  });
}

/*修改office类型的正文*/
async function updateOfficeContent(bodyType){
  const zwIframe = window._getZWIframe();
  const {currentUser: {id: userId}={}} =useBaseInfo.baseInfo.businessData||{};
  summaryTab.changeActiveIndex(0);
  await editOffice({bodyType, zwIframe, userId, canSign: useBaseInfo.canSign})
}

export default function () {
  return new Promise((resolve) => {
    const {bodyType} = useBaseInfo.baseInfo?.businessData?.summary||{};
    if (`${bodyType}`===BodyTypeEnums.html.toString()){
      updateHtmlContent();
      resolve();
    }else {
      updateOfficeContent(bodyType)
      .then(()=>resolve())
    }
  });
}
